package ilender.facebook;

import lombok.Getter;

import ilender.models.BaseModel;

/**
 * Handler for a Facebook notification.
 */
public class FBNotification {

	public final FBUser fbUser;

	@Getter private String template  ;
	@Getter private String reference;

	/**
	 * Constructor.
	 *
	 * @param fbUser Guess what that do...
	 */
	public FBNotification(FBUser fbUser) {
		this.fbUser  = fbUser ;
	}

	public FBNotification setTemplate(String template){
		this.template=template;
		return this;
	}

	public FBNotification setReference(String reference){
		this.reference=reference;
		return this;
	}

	/**
	 * Sets the reference using an object type name and a model id.
	 */
	public FBNotification setReference(String referenceType,long referenceId){
		return setReference(String.format("%s#%s",referenceType,referenceId));
	}

	/**
	 * Sets the reference using a model object.
	 *
	 * This will cause the notification to redirect the user directly to the model object.
	 *
	 * Only available for offers and items.
	 *
	 * @see ilender.controller.NavigationController#home
	 */
	public FBNotification setReference(BaseModel model){
		return setReference(model.getClass().getSimpleName(),model.getId());
	}

	/**Posts the notification on Facebook.*/
	public void post(){
		fbUser.graphAPI.makePostRequest(
				String.format("%s/notifications",fbUser.id),
				"template" , template,
				"ref", reference);
	}

}
